home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Examples / Text / Sources / EmbedRun.cpp next >
Encoding:
Text File  |  1994-04-21  |  14.9 KB  |  564 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                            EmbedRun.cpp
  4. //    Release Version:    $ 1.0d1 $
  5. //
  6. //    Author:                        Anthone Burbidge
  7. //    Creation Date:        3/28/94
  8. //
  9. //    Copyright:    © 1993, 1994 by Apple Computer, Inc., all rights reserved.
  10. //
  11. //========================================================================================
  12.  
  13. #ifndef _EMBEDRUN_
  14. #include "EmbedRun.h"
  15. #endif
  16.  
  17. #ifndef _TEXTPART_
  18. #include "TextPart.h"
  19. #endif
  20.  
  21. #ifndef _TEXTFACET_
  22. #include "TextFacet.h"
  23. #endif
  24.  
  25. #ifndef _TEXTFRAME_
  26. #include "TextFrame.h"
  27. #endif
  28.  
  29. #ifndef _TEXTPROXY_
  30. #include "TextProxy.h"
  31. #endif
  32.  
  33. #ifndef _VALUESTREAM_
  34. #include "ValueStream.h"
  35. #endif
  36.  
  37. #ifndef _TEXTSELC_
  38. #include "TextSelc.h"
  39. #endif
  40.  
  41. #ifndef _ODTEXT_
  42. #include "ODText.h"
  43. #endif
  44.  
  45. // ----- Textension Includes -----
  46.  
  47. #ifndef _Frames_
  48. #include "Frames.h"
  49. #endif
  50.  
  51. // ----- Framework Includes -----
  52.  
  53. #ifndef FWUTIL_H
  54. #include "FWUtil.h"
  55. #endif
  56.  
  57. #ifndef FWFRAME_H
  58. #include "FWFrame.h"
  59. #endif
  60.  
  61. // ----- OpenDoc Includes -----
  62.  
  63. #ifndef _INFO_
  64. #include <Info.h>
  65. #endif
  66.  
  67. #ifndef _CANVAS_
  68. #include <Canvas.h>
  69. #endif
  70.  
  71. #ifndef _FACET_
  72. #include <Facet.h>
  73. #endif
  74.  
  75. #ifndef _FRAME_
  76. #include <Frame.h>
  77. #endif
  78.  
  79. #ifndef _PART_
  80. #include <Part.h>
  81. #endif
  82.  
  83. #ifndef _SHAPE_
  84. #include <Shape.h>
  85. #endif
  86.  
  87. #ifndef _STORAGEU_
  88. #include <StorageU.h>
  89. #endif
  90.  
  91. #ifndef _DRAFT_
  92. #include <Draft.h>
  93. #endif
  94.  
  95. #ifndef _XMPSESSM_
  96. #include <XMPSessM.h>
  97. #endif
  98.  
  99. #ifndef _WINDOW_
  100. #include <Window.h>
  101. #endif
  102.  
  103. // ----- Macintosh Includes -----
  104.  
  105. #ifndef __FIXMATH__
  106. #include <FixMath.h>
  107. #endif
  108.  
  109. #ifndef mathRoutinesIncludes
  110. #include <Math Routines.h>
  111. #endif
  112.  
  113. #ifndef __DRAG__
  114. #include <Drag.h>
  115. #endif
  116.  
  117. #ifndef __EVENTS__
  118. #include <Events.h>
  119. #endif
  120.  
  121. #pragma segment TextPartSegment
  122.  
  123. //========================================================================================
  124. // CLASS CEmbeddedRun
  125. //========================================================================================
  126.  
  127. //----------------------------------------------------------------------------------------
  128. // CEmbeddedRun::CEmbeddedRun
  129. //----------------------------------------------------------------------------------------
  130.  
  131. CEmbeddedRun::CEmbeddedRun() :
  132.     fProxyRun(NULL)
  133. {
  134. }
  135.  
  136. //----------------------------------------------------------------------------------------
  137. // CEmbeddedRun::InitEmbeddedRun
  138. //----------------------------------------------------------------------------------------
  139.  
  140. void CEmbeddedRun::InitEmbeddedRun()
  141. {
  142.     InitBetterGraphicsRun(kSelectionWidth);
  143. }
  144.  
  145. //----------------------------------------------------------------------------------------
  146. // CEmbeddedRun::~CEmbeddedRun
  147. //----------------------------------------------------------------------------------------
  148.  
  149. CEmbeddedRun::~CEmbeddedRun()
  150. {
  151. }
  152.  
  153. //----------------------------------------------------------------------------------------
  154. // CEmbeddedRun::CreateNew
  155. //----------------------------------------------------------------------------------------
  156.  
  157. CAttrObject* CEmbeddedRun::CreateNew() const
  158. {
  159.     CEmbeddedRun* attrObject = new CEmbeddedRun();
  160.     if (attrObject)
  161.         attrObject->InitEmbeddedRun();
  162.     
  163.     return attrObject;
  164. }
  165.  
  166. //----------------------------------------------------------------------------------------
  167. // CEmbeddedRun::GetClassId
  168. //----------------------------------------------------------------------------------------
  169.  
  170. ClassId CEmbeddedRun::GetClassId() const
  171. {
  172.     return kEmbeddedPartClassId;
  173. }
  174.  
  175. //----------------------------------------------------------------------------------------
  176. // CEmbeddedRun::GetAttrIDs
  177. //----------------------------------------------------------------------------------------
  178.  
  179. void CEmbeddedRun::GetAttrIDs(short* countAttr, AttrId* attrArray) const
  180. {
  181.     CBetterGraphicsRun::GetAttrIDs(countAttr, attrArray);
  182.     
  183.     attrArray[(*countAttr)++] = kProxyRunAttr;
  184. }
  185.  
  186. //----------------------------------------------------------------------------------------
  187. // CEmbeddedRun::FreeData
  188. //----------------------------------------------------------------------------------------
  189.  
  190. void CEmbeddedRun::FreeData()
  191. {
  192.     short svOrgX = XMPASLMQDGlobals.thePort->portRect.left;
  193.     short svOrgY = XMPASLMQDGlobals.thePort->portRect.top;
  194.  
  195.     BC_TDynamicCollection<XMPFrame*, BC_CUnmanaged> tempCollect(4);
  196.  
  197.     BC_TCollectionActiveIterator<FW_CProxyFrame *> ite(*fProxyRun->GetProxyFrameList());
  198.     for (; !ite.IsDone(); ite.Next())
  199.     {
  200.         FW_CProxyFrame *frame = *ite.CurrentItem();
  201.         if (frame->GetContainingFrame() != NULL)
  202.             tempCollect.Append(frame->GetXMPFrame());
  203.     }    
  204.     
  205.     BC_TCollectionActiveIterator<XMPFrame *> ite2(tempCollect);
  206.     for (; !ite2.IsDone(); ite2.Next())
  207.     {
  208.         fProxyRun->GetEmbeddingPart()->RemoveEmbeddedFrame(*ite2.CurrentItem());
  209.     }
  210.  
  211.     ::SetOrigin(svOrgX, svOrgY);
  212.     
  213.     // Should delete fProxyRun is empty
  214. }
  215.  
  216. //----------------------------------------------------------------------------------------
  217. // CEmbeddedRun::WriteToStream
  218. //----------------------------------------------------------------------------------------
  219.  
  220. OSErr CEmbeddedRun::WriteToStream(CStream* outputStream)
  221. {
  222.     CTextPart* textPart = (CTextPart *) fProxyRun->GetEmbeddingPart();
  223.     CTextSelection* selection = (CTextSelection *) textPart->GetSelection();
  224.     XMPStorageUnit* storage = ((CValueStream *) outputStream)->GetStorageUnit();
  225.     
  226.     OSErr err = CBetterGraphicsRun::WriteToStream(outputStream);
  227.     
  228.     if (selection->IsCloning())
  229.         fProxyRun->CloneTo(storage, selection->GetCommandFrame(),
  230.                              selection->GetCloneKind());
  231.     else
  232.         fProxyRun->Externalize(storage);
  233.     
  234.     Rect boundingBox;
  235.     fProxyRun->GetBoundingBox(boundingBox);
  236.     outputStream->WriteRect(&boundingBox);
  237.  
  238.     return noErr;
  239. }
  240.  
  241. //----------------------------------------------------------------------------------------
  242. // CEmbeddedRun::ReadFromStream
  243. //----------------------------------------------------------------------------------------
  244.  
  245. OSErr CEmbeddedRun::ReadFromStream(CStream* inputStream)
  246. {
  247.     CTextPart* textPart = CTextPart::GetCurrentPart();
  248.     CTextSelection* selection = (CTextSelection *) textPart->GetSelection();
  249.     XMPStorageUnit* storage = ((CValueStream *) inputStream)->GetStorageUnit();
  250.     
  251.     OSErr err = CBetterGraphicsRun::ReadFromStream(inputStream);
  252.  
  253.     XMPFrame* embeddedXMPFrame;
  254.     
  255.     fProxyRun = (CTextProxyRun *) textPart->NewProxyRun();
  256.  
  257.     if (selection->IsCloning())
  258.         embeddedXMPFrame = fProxyRun->CloneFrom(storage, selection->GetCloneKind());
  259.     else
  260.         fProxyRun->Internalize(storage);
  261.     
  262.     Rect boundingBox;
  263.     inputStream->ReadRect(&boundingBox);
  264.     fProxyRun->SetBoundingBox(boundingBox);
  265.     
  266.     if (selection->IsCloning())
  267.     {
  268.         // ----- Embed the part -----
  269.         Point ptTemp;
  270.         ::SetPt(&ptTemp, boundingBox.left, boundingBox.top);
  271.         XMPTransform* externalXForm = ::NewXMPTransform(ptTemp);
  272.         
  273.         textPart->EmbedFrame(embeddedXMPFrame, 
  274.                              fProxyRun,
  275.                              textPart->GetMainPresentation(), 
  276.                                externalXForm,
  277.                              FALSE);
  278.     }
  279.  
  280.     return noErr;
  281. }
  282.  
  283. //----------------------------------------------------------------------------------------
  284. // CEmbeddedRun::DrawSelection
  285. //----------------------------------------------------------------------------------------
  286.  
  287. void CEmbeddedRun::DrawSelection(char hiliteStat, const RunPositionPB* paramPb)
  288. {
  289.     fProxyRun->SetSelectState(hiliteStat == kSelOn);
  290.     
  291.     if (hiliteStat == kSelOff)
  292.         return;
  293.     
  294.     short selectionWidth = GetSelectionWidth();
  295.     
  296.     Rect objRect;
  297.     this->GetObjectRect(paramPb, &objRect);
  298.     InsetRect(&objRect, -selectionWidth, -selectionWidth);
  299.     
  300.     PenState savedPen;
  301.     GetPenState(&savedPen);
  302.     
  303.     PenMode(patXor);
  304.     
  305.     ConstPatternParam pat = (hiliteStat == kSelInactiv) ? &ASLMQDGlobals.gray : &ASLMQDGlobals.black;
  306.     PenPat(pat);
  307.     
  308.     FrameRect(&objRect);
  309.     
  310.     Rect handleRect;
  311.     SetRect(&handleRect,
  312.             objRect.right - selectionWidth - 1, objRect.bottom - selectionWidth - 1,
  313.             objRect.right - 1, objRect.bottom - 1);
  314.     
  315.     InvertRect(&handleRect);
  316.     
  317.     SetPenState(&savedPen);
  318. }
  319.  
  320. //----------------------------------------------------------------------------------------
  321. // CEmbeddedRun::DoClick
  322. //----------------------------------------------------------------------------------------
  323.  
  324. void CEmbeddedRun::DoClick(ClickPB* clickPB, TClickCommandInfo* clickInfo, Boolean* abortDrag)
  325. {
  326.     *abortDrag = true;
  327.     if (this->PointInResizeHandle(clickPB))
  328.         this->Resize(clickPB);
  329.     else
  330.     {
  331.         CTextPart* textPart = (CTextPart *) fProxyRun->GetEmbeddingPart();
  332.         XMPEventData event = textPart->GetEmbeddedRunEvent();
  333.         CTextFacet* facet = textPart->GetEmbeddedRunFacet();
  334.         textPart->GetSelection()->Drag(facet, event);
  335.     }
  336. }
  337.  
  338. //----------------------------------------------------------------------------------------
  339. // CEmbeddedRun::PointInResizeHandle
  340. //----------------------------------------------------------------------------------------
  341.  
  342. Boolean CEmbeddedRun::PointInResizeHandle(ClickPB* clickPB) const
  343. {
  344.     CEmbeddedRun *self = (CEmbeddedRun *) this;
  345.     
  346.     Rect objRect;
  347.     self->GetObjectRect(&clickPB->positionPB, &objRect);
  348.  
  349.     short selectionWidth = GetSelectionWidth();
  350.     
  351.     Rect handleRect;
  352.     ::SetRect(&handleRect,
  353.               objRect.right - selectionWidth - 1, objRect.bottom - selectionWidth - 1,
  354.               objRect.right, objRect.bottom);
  355.     
  356.     return ::PtInRect(clickPB->clickPoint, &handleRect);
  357. }
  358.  
  359. //----------------------------------------------------------------------------------------
  360. // CEmbeddedRun::Resize
  361. //----------------------------------------------------------------------------------------
  362.  
  363. void CEmbeddedRun::Resize(ClickPB* clickPB)
  364. {
  365.     CTextFacet *facet = ((CTextPart *) fProxyRun->GetEmbeddingPart())->GetEmbeddedRunFacet();
  366.     
  367.     GrafPtr savedPort;
  368.     ::GetPort(&savedPort);
  369.     ::SetPort(facet->GetXMPWindow()->GetPlatformWindow());
  370.     
  371.     Point tmpPt = clickPB->clickPoint;
  372.     ::LocalToGlobal(&tmpPt);
  373.     
  374.     if (!::WaitMouseMoved(tmpPt))
  375.         return;
  376.  
  377.     Rect objRect;
  378.     this->GetObjectRect(&clickPB->positionPB, &objRect);
  379.     ::InsetRect(&objRect, -GetSelectionWidth(), -GetSelectionWidth());
  380.  
  381.     ::PenNormal();    
  382.     ::PenPat(&XMPASLMQDGlobals.gray);
  383.     ::PenMode(patXor);
  384.     ::PenSize(1, 1);
  385.  
  386.     Point curPt;
  387.     Point lastPt = clickPB->clickPoint;
  388.     while (::StillDown())
  389.     {
  390.         ::GetMouse(&curPt);
  391.         
  392.         this->DrawResizeFeedback(objRect);
  393.     
  394.         objRect.right += curPt.h - lastPt.h;
  395.         objRect.bottom += curPt.v - lastPt.v;
  396.         
  397.         this->DrawResizeFeedback(objRect);
  398.         
  399.         lastPt = curPt;
  400.     }
  401.     
  402.     ::PenNormal();    
  403.  
  404.     ::SetPort(savedPort);
  405.     
  406.     this->ChangeShape(objRect);
  407. }
  408.  
  409. //----------------------------------------------------------------------------------------
  410. // CEmbeddedRun::DrawResizeFeedback
  411. //----------------------------------------------------------------------------------------
  412.  
  413. void CEmbeddedRun::DrawResizeFeedback(const Rect& rect)
  414. {
  415.     ::FrameRect(&rect);
  416. }
  417.  
  418. //----------------------------------------------------------------------------------------
  419. // CEmbeddedRun::AttributeToBuffer
  420. //----------------------------------------------------------------------------------------
  421.  
  422. void CEmbeddedRun::AttributeToBuffer(AttrId theAttr, void* attrBuffer) const
  423. {
  424.     if (theAttr == kProxyRunAttr)
  425.         *longPtr(attrBuffer) = long(fProxyRun);
  426.     else
  427.         CBetterGraphicsRun::AttributeToBuffer(theAttr, attrBuffer);
  428. }
  429.  
  430. //----------------------------------------------------------------------------------------
  431. // CEmbeddedRun::BufferToAttribute
  432. //----------------------------------------------------------------------------------------
  433.  
  434. void CEmbeddedRun::BufferToAttribute(AttrId theAttr, const void* attrBuffer)
  435. {
  436.     if (theAttr != kProxyRunAttr)
  437.         CBetterGraphicsRun::BufferToAttribute(theAttr, attrBuffer);
  438.     else
  439.         fProxyRun = *((CTextProxyRun **) attrBuffer);
  440. }
  441.  
  442. //----------------------------------------------------------------------------------------
  443. // CEmbeddedRun::GetAttrFlags
  444. //----------------------------------------------------------------------------------------
  445.  
  446. long CEmbeddedRun::GetAttrFlags(AttrId theAttr, void* attrBuffer, long updateMessage /*=0*/) const
  447. {
  448.     long flags = (theAttr == kProxyRunAttr) ? kReformat | kVReformat : 0;
  449.     
  450.     return flags | CBetterGraphicsRun::GetAttrFlags(theAttr, attrBuffer, updateMessage);
  451. }
  452.  
  453. //----------------------------------------------------------------------------------------
  454. // CEmbeddedRun::GetDimensions
  455. //----------------------------------------------------------------------------------------
  456.  
  457. Boolean CEmbeddedRun::GetDimensions(short* hite, short* width)
  458. {
  459.     Rect rt;
  460.     fProxyRun->GetBoundingBox(rt);
  461.     
  462.     *hite = rt.bottom - rt.top;
  463.     *width = rt.right - rt.left;
  464.     
  465.     return true;
  466. }
  467.  
  468. //----------------------------------------------------------------------------------------
  469. // CEmbeddedRun::Draw
  470. //----------------------------------------------------------------------------------------
  471.  
  472. void CEmbeddedRun::Draw(const Rect* theRect)
  473. {
  474.     XMPShape *shape = ::NewXMPShape(*theRect);
  475.         
  476.     CTextFacet *facet = ((CTextPart *) fProxyRun->GetEmbeddingPart())->GetEmbeddedRunFacet();
  477.         
  478.     XMPShape *invalidShape = ::NewXMPShape(*theRect);
  479.     shape = ::NewXMPShape();
  480.     
  481.     FW_CEmbeddedXMPFacetsIterator ite(fProxyRun, facet ,kXMPFrontToBack);
  482.     for (XMPFacet *embeddedXMPFacet = ite.First(); ite.IsNotComplete(); embeddedXMPFacet = ite.Next())
  483.     {
  484.         shape->CopyFrom(invalidShape);
  485.         shape->Transform(facet->GetFrameTransform());                        // Frame -> Window
  486.         shape->InverseTransform(embeddedXMPFacet->GetFrameTransform());     // Window -> Frame
  487.  
  488.         embeddedXMPFacet->Draw(shape);
  489.         embeddedXMPFacet->DrawChildren(shape);
  490.     }
  491.     
  492.     facet->Validate(NULL);
  493.         
  494.     delete invalidShape;
  495.     delete shape;
  496. }
  497.  
  498. //----------------------------------------------------------------------------------------
  499. // CEmbeddedRun::ChangePosition
  500. //----------------------------------------------------------------------------------------
  501.  
  502. void CEmbeddedRun::ChangePosition(const Point& pt)
  503. {
  504.     Rect rt;
  505.     fProxyRun->GetBoundingBox(rt);
  506.     
  507.     if (rt.top == pt.v && rt.left == pt.h)
  508.         return;
  509.         
  510.     short height = rt.bottom - rt.top;
  511.     short width = rt.right - rt.left;
  512.     
  513.     rt.top = pt.v;
  514.     rt.left = pt.h;
  515.     rt.bottom = rt.top + height;
  516.     rt.right = rt.left + width;
  517.     
  518.     fProxyRun->SetBoundingBox(rt);
  519.     
  520.     XMPShape *shape = ::NewXMPShape(rt);
  521.  
  522.     FW_CProxyFrame* proxy = fProxyRun->WhichProxyFrame(((CTextPart *) fProxyRun->GetEmbeddingPart())->GetEmbeddedRunFrame());
  523.  
  524.     short svOrgX = XMPASLMQDGlobals.thePort->portRect.left;
  525.     short svOrgY = XMPASLMQDGlobals.thePort->portRect.top;
  526.  
  527.     proxy->ResizeFrame(shape);
  528.  
  529.     ::SetOrigin(svOrgX, svOrgY);
  530. }
  531.  
  532. //----------------------------------------------------------------------------------------
  533. // CEmbeddedRun::ChangeShape
  534. //----------------------------------------------------------------------------------------
  535.  
  536. void CEmbeddedRun::ChangeShape(const Rect& rt)
  537. {
  538.     fProxyRun->SetBoundingBox(rt);
  539.     
  540.     XMPShape *shape = ::NewXMPShape(rt);
  541.  
  542.     FW_CProxyFrame* proxy = fProxyRun->WhichProxyFrame(((CTextPart *) fProxyRun->GetEmbeddingPart())->GetEmbeddedRunFrame());
  543.  
  544.     short svOrgX = XMPASLMQDGlobals.thePort->portRect.left;
  545.     short svOrgY = XMPASLMQDGlobals.thePort->portRect.top;
  546.  
  547.     proxy->ResizeFrame(shape);
  548.  
  549.     ::SetOrigin(svOrgX, svOrgY);
  550.     
  551.     // ----- Reflow the text
  552.     
  553.     COpenDocText* textEdit = ((CTextPart *) fProxyRun->GetEmbeddingPart())->GetEditText();
  554.     
  555.     CDisplayChanges displayChanges;
  556.     displayChanges.SetChangesKind(kNeedVReformat | kNeedHReformat);
  557.     
  558.     short action = textEdit->DisplayChanged(&displayChanges);
  559.     
  560.     // ------ Notify embedded parts of position changes.
  561.     
  562.     textEdit->NotifyEmbeddedPartsOfPositionChange(0, textEdit->CountLines() - 1);
  563. }
  564.